package com.txunda.zhuohired.fgt.manage.home;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.ants.theantsgo.gson.GsonUtil;
import com.ants.theantsgo.util.L;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.adapter.manage.home.ManageAllAdapter;
import com.txunda.zhuohired.aty.manage.home.ManageAllAty;
import com.txunda.zhuohired.aty.manage.home.ManageAllDetailsAty;
import com.txunda.zhuohired.aty.manage.home.PostJobAty;
import com.txunda.zhuohired.base.BaseFgt;
import com.txunda.zhuohired.bean.manage.home.ManageAllBean;
import com.txunda.zhuohired.http.pst.CompanyPst;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/3/10 16:20
 * 功能描述：全职
 * 联系方式：jiahuanchen@126.com
 */

public class ManageAllFgt extends BaseFgt {

    @ViewInject(R.id.lv_fgt_manage_all)
    private ListView lv_fgt_manage_all;

    private ManageAllAdapter manageAllAdapter;
    private CompanyPst companyPst;
    private String token;
    private String popType="";
    private int type = 1;
    private int page = 1;
    private List<ManageAllBean.DataBean> manageAllList;
    // 2.1 定义用来与外部activity交互，获取到宿主activity
    private FragmentInteraction listterner;
    @Override
    protected void immersionInit() {

    }

    @Override
    @OnClick({R.id.tv_all_post})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_all_post://发布新职位
                startActivity(PostJobAty.class, null);
                break;
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fgt_manage_all;
    }



    @Override
    protected void initialized() {
        companyPst = new CompanyPst(this);
        token = application.getUserInfo().get("data");
        manageAllList = new ArrayList<>();

    }

    @Override
    protected void requestData() {
    }
    public void initHttp(){
        companyPst.isjobList(token,String.valueOf(type),String.valueOf(page),popType);
    }
    private void initAdapter() {
        manageAllAdapter = new ManageAllAdapter(getActivity(),manageAllList);
        lv_fgt_manage_all.setAdapter(manageAllAdapter);
        lv_fgt_manage_all.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Bundle bundle = new Bundle();
                bundle.putString("type", String.valueOf(i));
                startActivity(ManageAllDetailsAty.class, bundle);
            }
        });
    }



    public static ManageAllFgt newInstance(int type) {
        ManageAllFgt fragment = new ManageAllFgt();
        fragment.type = type;
        return fragment;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        popType = ((ManageAllAty)activity).getPopType();
        if(activity instanceof FragmentInteraction) {
            listterner = (FragmentInteraction)activity; // 2.2 获取到宿主activity并赋值
        } else{
            throw new IllegalArgumentException("activity 必须 implements FragmentInteraction");
        }
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        if(requestUrl.contains("isjobList")){
            L.e("isjobList",jsonStr);
            ManageAllBean manageAllBean = GsonUtil.GsonToBean(jsonStr,ManageAllBean.class);

            manageAllList = manageAllBean.getData();

            initAdapter();


        }
    }

    /**
     * 每次fragment可见时
     * 请求接口
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            new Handler().postDelayed(new Runnable() {
                public void run() {
//                    if(listterner!=null){
//                        listterner.process(String.valueOf(type));
//                    }
                    initHttp();
                }
            }, 100);
        } else {
            // 不可见时不执行操作
        }
    }

    // 1 定义了所有activity必须实现的接口方法
    public interface FragmentInteraction {
        void process(String str);
    }
    //把传递进来的activity对象释放掉
    @Override
    public void onDetach() {
        super.onDetach();
        listterner = null;
    }


}
